<!-- src/views/RentalStep2.vue -->
<template>
  <div class="rental-form">
    <!-- 顶部导航 -->
    <div class="nav-header">
      <div class="back">
        <i class="el-icon-arrow-left"  @click="goBack"></i>
        <span>我要转租</span>
      </div>
      <div class="icons">
        <el-button type="text" icon="el-icon-more"></el-button>
        <el-button type="text" icon="el-icon-s-help"></el-button>
      </div>
    </div>

    <!-- 进度指示器 -->
    <div class="step-indicator">
      <div class="step completed">1</div>
      <div class="line completed"></div>
      <div class="step completed">2</div>
      <div class="line completed"></div>
      <div class="step active">3</div>
    </div>

    <!-- 表单内容 -->
    <div class="form-content">
      <!-- 微信号 -->
      <div class="form-item">
        <div class="form-title">这是我的微信号</div>
        <el-input
            v-model="formData.wechat"
            placeholder="例如：yujian2020">
        </el-input>
      </div>

      <!-- 手机号 -->
      <div class="form-item">
        <div class="form-title">这是我的手机号，快点联系我吧</div>
        <el-input
            v-model="formData.phone"
            placeholder="11位电话号码">
        </el-input>
      </div>

      <!-- 入住时间 -->
      <div class="form-item">
        <div class="form-title">在这些时间看房我比较方便</div>
        <div class="select-group">
          <el-select v-model="formData.visitingTime">
            <el-option value="最好午休、下班以后和周末"></el-option>
            <el-option value="随时可以看房，我能赶过去"></el-option>
            <el-option value="我不带看房，有其他人负责"></el-option>
          </el-select>
        </div>

      </div>

      <!-- 有效期 -->
      <div class="form-item">
        <div class="form-title">出租有效期到这一天</div>
        <el-date-picker
            v-model="formData.endTime"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
            style="width: 100%">
        </el-date-picker>
      </div>

      <!-- 标题 -->
      <div class="form-item">
        <div class="form-title">最后，来个亮眼的标题吧</div>
        <el-input
            v-model="formData.title"
            placeholder="比如：首城国际全新三居室价出租">
        </el-input>
      </div>

      <!-- 按钮组 -->
      <div class="button-group">
        <el-button @click="handlePrev">上一步</el-button>
        <el-button type="primary" @click="handleSubmit">完成</el-button>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  name: 'SubletStep3',
  data() {
    return {
      formData: {
        wechat: '',
        phone: '',
        visitingTime: '最好午休、下班以后和周末',
        endTime: '',
        title: ''
      },
      submitting: false
    }
  },
  created() {
    if (this.$store && this.$store.state.formData) {
      const savedData = this.$store.state.formData.sublet3
      if (savedData) {
        this.formData = {...savedData}
      }
    }
  },
  methods: {
    handlePrev() {
      try {
        if (this.$store) {
          this.$store.commit('SET_SUBLET3_DATA', this.formData)
        }
        this.$router.push('/subletStep2')
      } catch (error) {
        console.error('Error in handlePrev:', error)
        this.$message.error('返回上一步失败')
      }
    },
    async handleSubmit() {
      if (this.submitting) return

      try {
        this.submitting = true

        // 表单验证
        if (!this.validateForm()) {
          return
        }

        // 保存当前步骤数据到 Vuex
        if (this.$store) {
          this.$store.commit('SET_SUBLET3_DATA', this.formData)
        }

        // 准备提交的完整数据
        const submitData = {
          ...this.$store.state.formData.sublet1,
          ...this.$store.state.formData.sublet2,
          ...this.$store.state.formData.sublet3
        }
        // 发送请求
        this.axios.post('http://localhost:9701/web/house/addSubletHouse', submitData).then(response => {
          if (response.data.code === 200) {
            this.$message.success('提交成功！')
            // 重置表单
            this.resetForm()
            // 可以选择跳转到成功页面或列表页
            // this.$router.push('/success').
            // 跳转到发布页面
            setTimeout(() => { this.$router.push('/release') }, 1000)
          } else {
            this.$message.error(response.data.message)
          }
        }).catch(error => {
          console.error('Error:', error)
        })
      } catch (error) {
        console.error('Submit error:', error)
        this.$message.error(error.response?.data?.message || '提交失败，请重试')
      } finally {
        this.submitting = false
      }
    },
    validateForm() {
      // 表单验证逻辑
      if (!this.formData.wechat) {
        this.$message.warning('请输入微信号')
        return false
      }
      if (!this.formData.phone) {
        this.$message.warning('请输入手机号')
        return false
      }
      if (!this.formData.endTime) {
        this.$message.warning('请选择出租有效期')
        return false
      }
      if (!this.formData.title) {
        this.$message.warning('请输入标题')
        return false
      }
      // 手机号格式验证
      const phoneReg = /^1[3-9]\d{9}$/
      if (!phoneReg.test(this.formData.phone)) {
        this.$message.warning('请输入正确的手机号格式')
        return false
      }

      return true
    },
    goBack(){
      this.$router.push('/release')
    },
    resetForm() {
      this.$store.commit('SET_SUBLET3_DATA', {
        wechat: '',
        phone: '',
        visitingTime: '最好午休、下班以后和周末',
        endTime: '',
        title: ''
      })
      this.$store.commit('SET_SUBLET1_DATA', {
        longitude: '',
        latitude: '',
        location: '',
        imagesUrl: '',
        videoUrl: '',
        nearbyStation: '',
        structure: '',
        male: 0,
        female: 0
      })
      this.$store.commit('SET_SUBLET2_DATA', {
        introduce: '',
        rent: null,
        tenancyTerm: 3,
        payType: '押一付一',
        renterRequire: '',
        landlord:'',
        contractExpiration: '',
        moveTime: ''
      })
    }
  }
}
</script>


<style scoped>
.select-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.icons {
  display: flex;
  border: 1px solid white;
  padding: 5px 5px;
  border-radius: 15px;
}

.icons button {
  color: white;
  padding: 0 5px;
  font-size: 18px;
}

.rental-form {
  max-width: 414px;
  margin: 0 auto;
  background: #f5f5f5;
}

.nav-header {
  background: #ff7f50;
  color: white;
  padding: 10px 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.step-indicator {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px 0;
}

.el-button.is-loading {
  pointer-events: none;
}

.step {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #ddd;
}

.step.active {
  background: #409EFF;
  color: white;
}

.step.completed {
  background: #67C23A;
  color: white;
}

.line {
  width: 100px;
  height: 1px;
  background: #ddd;
  margin: 0 10px;
}

.line.completed {
  background: #67C23A;
}

.form-content {
  padding: 15px;
}

.form-item {
  background: white;
  padding: 15px;
  margin-bottom: 10px;
  border-radius: 8px;
}

.form-title {
  margin-bottom: 10px;
  color: #333;
}

.button-group {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
}

:deep(.el-button) {
  width: 45%;
}
</style>
